#!/usr/bin/env bash

#开启哪个服务
all_server=(1 2 3 4 5)

function build(){
    rm -rf ../../pkg
    go build -a
}


function start(){
    if [ $# -ge 2 ]; then
        echo "param num:" $#
        echo "param is:" $*
        for arg in $*
        do
            echo "the param is:" $arg
            if [ $arg != "start" ]; then
                echo "start the server id is:" $arg
                nohup ./chess_platform start  --pid $arg &
                if [ $? == 0 ]; then
                    echo "start the server_id[${arg}] successfully"
                else
                    echo "start the server_id[${arg}] fail"
                fi
            fi
        done
    else
        echo "start all server"
        len=${#all_server[@]}
        for ((i=0;i<${len};i++))
        do
            pid=${all_server[$i]}
            echo "start the server id is:" $pid
            nohup ./chess_platform start  --pid $pid &
            if [ $? == 0 ]; then
                echo "start the server_id[${pid}] successfully"
            else
                echo "start the server_id[${pid}] fail"
            fi
        done
    fi
    return 0
}

function stop(){
    if [ $# -ge 2 ]; then
        echo "param num:" $#
        echo "param is:" $*
        for arg in $*
        do
            echo "the param is:" $arg
            if [ $arg != "stop" ]; then
                echo "stop the server id  is:" $arg
                cat chess_platform_${arg}.pid | xargs kill -9
                if [ $? == 0 ]; then
                    echo "stop the server_id[${arg}] successfully"
                else
                    echo "stop the server_id[${arg}] fail"
                fi
            fi
        done
    else
        echo "stop all server"
        len=${#all_server[@]}
        for ((i=0;i<${len};i++))
        do
          pid=${all_server[$i]}
          echo "stop the server id is:" $pid
          cat chess_platform_${pid}.pid | xargs kill -9
          if [ $? == 0 ]; then
              echo "stop the server_id[${pid}] successfully"
          else
              echo "stop the server_id[${pid}] fail"
          fi
        done
    fi
    return 0
}

function restart(){
    stop "stop" $@
    start "start" $@
}

#check the server exist or not, if not,auto restart it
function checkServer(){
    while true
    do
        #echo "check all server"
        len=${#all_server[@]}
        for ((i=0;i<${len};i++))
        do
            server_id=${all_server[$i]}
            pid=`cat chess_platform_${server_id}.pid`
            #echo "get the server pid is:"${pid}
            pid_num=`ps ax |  grep ${pid} | grep -v grep | awk '{ print $1 }' | wc -l`
            if [ $pid_num -lt 1 ]; then
                #echo "the server id [${server_id}] already dead,pid[${pid}]"
                # 加上-d表示是被拉取来的，说明服务器挂了
                #nohup ./chess_platform start   --pid $server_id &
                #暂时注释了，正式发布时要启用下条命令
                nohup ./chess_platform start -d  --pid $server_id &
            #else
                #echo "the server id [${server_id}] still alive,pid[${pid}]"
            fi
        done
    sleep 30
    done
}


case "$1" in
    build)
        build $@
    ;;
    start)
        start $@
    ;;
    stop)
        stop $@
    ;;
    restart)
        restart $@
    ;;
    check)
        checkServer $@
    ;;
    *)
      echo $"Usage: $0 {start|stop|restart|build|check}"
      echo "step 1: build the project"
      echo "step 2: start the project"
      echo "step 3: check the project"
      exit 2
esac
exit $?